package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import java.time.LocalDateTime;
import java.util.Date;
import javax.naming.Name;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/27 16:50
 * @description:
 */
@Controller
@RequestMapping("/user")
public class UserController {

  @Autowired
  private UserService userService;

  @RequestMapping("/test")
  @ResponseBody
  public ServerResponse test() {
    User user = new User();
    user.setId(1);
    user.setUsername("hh");
    user.setPassword("pishf");
    user.setEmail("aa@mail.com");
    user.setQuestion("hello");
    user.setAnswer("word");
    user.setRole(0);
    user.setCreateTime(LocalDateTime.now());
    user.setUpdateTime(LocalDateTime.now());

    return ServerResponse.createSuccess(user);
  }


  @RequestMapping("/login")
  @ResponseBody
  public ServerResponse login(
      @RequestParam(name = "username") String username,
      @RequestParam(name = "password") String password,
      HttpSession session) {
    // userService
    // if success -> ServerResponse<User>
    // if error   -> ServerResponse.message = 密码错误
    ServerResponse<User> response = userService.login(username, password);
    if (response.success()) {
      // save to session
      session.setAttribute("user", response.getData());
    }

    return response;
  }


  public ServerResponse checkVaild(String str, String type) {
    // to service
    userService.vaild(str, type);
    return null;
  }
}
